{block name=body}

<div class="row">
	<div class="col-sm-12">
		<table class="table table-bordered table-striped table-hover" style="margin-top:40px;">
			<tbody>
				<tr>
					<td><b>Order no:</b></td>
					<td class="text-center">{$fields.payment.0.payment_transaction_no}</td>
					<td><b>Order placed:</b></td>
					<td class="text-center">{$fields.cart_closed_date|date_format:"%e %B %Y"}</td>
				</tr>
				<tr>
					<td><b>User's detail:</b></td>
					<td class="text-center">{if $fields.user}<a href="/admin/members/existing/{$fields.user.0.user_id}">{$fields.user.0.user_gname} {$fields.user.0.user_surname} / {$fields.user.0.user_email}</a>{/if}</td>
					<td><b>Payment status:</b></td>
					<td class="text-center">
					 <b id="payment-status-label">{if $fields.payment.0.payment_status eq 'P'}PENDING{elseif $fields.payment.0.payment_status eq 'A'}APPROVED{elseif $fields.payment.0.payment_status eq 'R'}REFUNDED{elseif $fields.payment.0.payment_status eq 'C'}CANCELLED{else}{$fields.payment.0.payment_status}{/if}</b>
				</tr>
				<tr>
					<td><b>Shipping method:</b></td>
					<td class="text-center">{$fields.payment.0.payment_shipping_method}</td>
					<td><b>Payment method:</b></td>
					<td class="text-center">{$fields.payment.0.payment_response_cardscheme}</td>
				</tr>
				<tr>
					<td><b>Address:</b></td>
					<td class="text-center">{$fields.payment.0.billing_address.0.address_name}</td>
					<td class="text-center" colspan="2">
						{$fields.payment.0.billing_address.0.address_line1} 
						{$fields.payment.0.billing_address.0.address_line2} 
						{$fields.payment.0.billing_address.0.address_suburb}, 
						{$fields.payment.0.billing_address.0.address_state}, 
						{$fields.payment.0.billing_address.0.address_country} 
						{$fields.payment.0.billing_address.0.address_postcode}. 
						{if $fields.payment.0.billing_address.0.address_telephone} {$fields.payment.0.billing_address.0.address_telephone}{/if}
						{if $fields.payment.0.billing_address.0.address_telephone && $fields.payment.0.billing_address.0.address_telephone}{/if} 
						{if $fields.payment.0.billing_address.0.address_mobile} / {$fields.payment.0.billing_address.0.address_mobile} {/if}
					</td>
				</tr>
				<tr>
					<td><b>Comments:</b></td>
					<td class="text-center" colspan="3">
						{$fields.payment.0.payment_shipping_comments} 
					</td>
				</tr>
			</tbody>
		</table>
	</div>
	
	<div class="col-sm-12">
		<table class="table table-bordered table-striped table-hover" style="margin-top:15px;">
			<thead>
				<tr>
					<th>Item</th>
					<th class="text-right">Qty</th>
					<th class="text-right">Unit Price</th>
					<th class="text-right">Subtotal</th>
				</tr>
			</thead>
			<tbody>
				{foreach $fields.items as $item}
				<tr>
					<td>{if $item.cartitem_product_gst eq '0'}*{/if}{if $item.cartitem_custom_type}{$item.cartitem_custom_type}: {/if}{$item.cartitem_product_name} 
						{if $item.attributes}
								{assign var='hasweek' value=0}
								{foreach from=$item.attributes item=attr name=attributesarr}
									{if $item.cartitem_class_flag eq 1 && $item.cartitem_enrolment_type eq 'T' } 
										<b>[ Week: {$item.attributes.0.cartitem_attr_attr_value_name} ]</b>
									{else}
										{if $attr.cartitem_attr_attribute_name eq 'week'}
											{if $hasweek eq 0}
												/ Week: 
												{assign var='hasweek' value=1}
											{/if}
											{$attr.cartitem_attr_attr_value_name} {if !$smarty.foreach.attributesarr.last}|{/if}
										{else}
											 / {$attr.cartitem_attr_attribute_name}: {$attr.cartitem_attr_attr_value_name}
										{/if}
									{/if}
								{/foreach}
							{/if}
		  			</td>
					<td class="text-right">{$item.cartitem_quantity}</td>
					<td class="text-right">{$CURRENCY_SYMBOL}{$item.cartitem_product_price|number_format:2:".":","}</td>
					<td class="text-right">{$CURRENCY_SYMBOL}{$item.cartitem_subtotal|number_format:2:".":","}</td>
				</tr>
				{/foreach} 
				<tr>
					<td class="text-right" colspan="3">Subtotal</td>
					<td class="text-right">{$CURRENCY_SYMBOL}{$fields.payment.0.payment_subtotal|number_format:2:".":","}</td>
				</tr>
				{if $fields.payment.0.payment_discount neq '0.00'}
				<tr>
					<td class="text-right" colspan="3">Discount {if $fields.cart_discount_code}<small>[Code: {$fields.cart_discount_code}]</small>{/if}</td>
					<td class="text-right">-${$fields.payment.0.payment_discount|number_format:2:".":","}</td>
				</tr>
				{/if}
				{if $fields.payment.0.payment_shipping_fee neq '0.00'}
				<tr>
					<td class="text-right" colspan="3">Shipping</td>
					<td class="text-right">{$CURRENCY_SYMBOL}{$fields.payment.0.payment_shipping_fee|number_format:2:".":","}</td>
				</tr>
				{/if}
				{if $fields.payment.0.payment_additional neq '0.00'}
				<tr>
					<td class="text-right" colspan="3">{$fields.payment.0.payment_additional_description}</td>
					<td class="text-right">{$CURRENCY_SYMBOL}{$fields.payment.0.payment_additional|number_format:2:".":","}</td>
				</tr>
				{/if}
				<tr>
					<td class="text-right" colspan="3">Incl. {$TAX_CODE}</td>
					<td class="text-right">(${$fields.payment.0.payment_gst|number_format:2:".":","})</td>
				</tr>
				{if $fields.payment.0.payment_surcharge neq '0.00'}
				<tr>
					<td class="text-right" colspan="3">Credit card surcharge</td>
					<td class="text-right">{$CURRENCY_SYMBOL}{$fields.payment.0.payment_surcharge|number_format:2:".":","}</td>
				</tr>
				{/if}
				<tr>
					<td class="text-right" colspan="3"><b>Total</b></td>
					<td class="text-right"><b> {$CURRENCY_SYMBOL}{$fields.payment.0.payment_total|number_format:2:".":","}</b></td>
				</tr>
				{if $fields.payment.0.payment_charged_amount neq $fields.payment.0.payment_total}
				<tr>
					<td class="text-right" colspan="3"><b>Initial payment [{$fields.payment.0.payment_created|date_format:"%Y-%m-%d"}] - {$fields.payment.0.payment_response_cardscheme}</b></td>
					<td class="text-right"><b> {$CURRENCY_SYMBOL}{$fields.payment.0.payment_charged_amount|number_format:2:".":","}</b></td>
				</tr>
				{assign var='hasPending' value=0}
				{foreach $fields.payment.0.debt as $debt}
					{if $debt.payment_id && $debt.debt_paid_date}
					<tr>
						<td colspan="3" class="text-right"><b>Payment [{$debt.debt_paid_date|date_format:"%Y-%m-%d"}] - {$debt.payment_response_cardscheme}</b></td>
						<td class="text-right"><b>{$CURRENCY_SYMBOL}{$debt.payment_charged_amount|number_format:2:".":","}</b></td>
					</tr>
					{else}
					{assign var='hasPending' value=$debt.debt_id}
					<tr class="danger">
						<td colspan="3" class="text-right"><b>Pending payment [{$debt.debt_nextpayment_date|date_format:"%Y-%m-%d"}]</b></td>
						<td class="text-right"><b>{$CURRENCY_SYMBOL}{$debt.debt_amount|number_format:2:".":","}</b></td>
					</tr>
					{/if}
				{/foreach}
				{/if}
			</tbody>
		</table>
	</div>
</div>
<div class="row">
	<form class="well form-horizontal" id="send_invoice_email" accept-charset="UTF-8" method="post">
<!--		<input type="hidden" value="{$fields.payment.0.payment_id}" name="payment_id" /> 
 		<input type="hidden" value="{$fields.payment.0.billing_address.0.address_id}" name="bill_ID" /> 
		<input type="hidden" value="{$fields.payment.0.shipping_address.0.address_id}" name="ship_ID" /> 
		<input type="hidden" value="{$fields.user.0.user_gname}" name="user[gname]" />
		<input type="hidden" value="{$fields.cart_id}" name="cart_id" />   -->
		<input type="hidden" value="{$fields.user.0.user_email}" name="email" id="email" /> 
		<input type="hidden" value="{$fields.payment.0.payment_invoice_email_id}" name="email_id" /> 
		<input type="hidden" name="formToken" id="formToken" value="{$token}" />
					
		<div class="row">
			<div class="col-sm-offset-3 col-sm-9">
				<a href="javascript:void(0);" onClick="sendInvoiceEmail();" id="send-btn" class="btn btn-info pull-right top-btn">Re-send Invoice</a>
				{if $hasPending gt 0}<a href="/admin/members/make-payment/{$fields.user.0.user_id}?debt_id={$hasPending}" class="btn btn-warning pull-right top-btn">Make payment</a>{/if}
			</div>
		</div>
	</form>
</div>

<div class="row">
	<form class="well form-horizontal" id="Edit_Record" accept-charset="UTF-8" method="post">
		{if $cnt eq ""}{assign var=cnt value=0}{/if} 
		<!-- <input type="hidden" value="order_id" name="field[1][tbl_order][{$cnt}][id]" id="id" /> 
		<input type="hidden" value="{$fields.payment.0.order.0.order_payment_id}" name="field[1][tbl_order][{$cnt}][order_payment_id]" id="order_payment_id">
		<input type="hidden" value="{$admin.id}" name="field[1][tbl_order][{$cnt}][order_admin_id]" id="order_admin_id"> -->
		
		<input type="hidden" value="payment_id" name="field[2][tbl_payment][{$cnt}][id]" id="id_payment" /> 
		<input type="hidden" value="{$fields.payment.0.payment_id}" name="field[2][tbl_payment][{$cnt}][payment_id]" id="payment_id">
		<input type="hidden" value="{$fields.payment.0.payment_status}" name="field[2][tbl_payment][{$cnt}][payment_status]" id="payment_status">
		
		<input type="hidden" name="formToken" id="formToken" value="{$token}" />
					
		<div class="row form-group">
			<label class="col-sm-3 control-label" for="id_cart_order_status">Order Status</label>
			<div class="col-sm-5">
				<select class="form-control" name="order_status_id" id="order_status_id">
					{foreach $fields.options.status as $opt}
							<option value="{$opt.id}" {if $fields.payment.0.order.0.order_status_id eq $opt.id}selected="selected"{/if}>{$opt.value}</option>
					{/foreach} 
				</select>
			</div>
		</div>
		<div class="row form-group">
			<div class="col-sm-offset-3 col-sm-9">
				<a href="javascript:void(0);" onClick="changeOrderStatus(false)" class="btn btn-primary pull-right top-btn"><span class="glyphicon glyphicon-floppy-saved"></span> 保存</a>
				<a href="javascript:void(0);" onClick="changeOrderStatus(true)" class="btn btn-info pull-right top-btn"><span class="glyphicon glyphicon-envelope"></span> Save and Notify</a>
			</div>
		</div>
	</form>
</div>


{include file='jquery-validation.tpl'}

<script type="text/javascript">

$(document).ready(function(){

	$('#Edit_Record').validate();
	
});


function changeOrderStatus(NOTIFY){
	var status = $('#order_status_id option:selected').text();
	$('#payment_status').val(status.substring(0, 1));
	var statusLabel = status.split(' ')[0];
	$('#payment-status-label').html(statusLabel.toUpperCase());
	
	$('#Edit_Record').submit();
	ChangeOrderStatus($('#payment_id').val(), $('#order_status_id').val() );
	if(NOTIFY){
		sendStatusEmail('email', 'payment_id', 'order_status_id');
	}
	
}


function ChangeOrderStatus(PAYMENTID, STATUS){
	$('body').css('cursor', 'wait');
	var datastring = 'action=ChangeOrderStatus&payment_id='+PAYMENTID+'&status='+STATUS+'&formToken='+$('#formToken').val();
	$.ajax({
		type : "POST",
		url : "/admin/includes/processes/processes-enrolment.php",
		cache : false,
		data : datastring,
		dataType : "html",
		success : function(data, textStatus) {
			try {
				var obj = $.parseJSON(data);
				
			} catch (err) {}
			$('body').css('cursor', 'default');
		},
		error: function(){
			$('body').css('cursor','default'); 
			console.log('AJAX error');
	  }
	});
}

function sendInvoiceEmail(){
	var datastring = $("#send_invoice_email").serialize();
	$('body').css('cursor','wait');
	$('#send-btn').addClass('disabled');
	$.ajax({
		type: "POST",
	    url: "/admin/includes/processes/send-invoice-email.php",
		cache: false,
		data: datastring,
		dataType: "html",
	    success: function(data) {
	    	try{
	    		var obj = $.parseJSON(data);
			 	if (obj.response) {
			 		$('#sent').slideDown();
					setTimeout(function(){
						$('#sent').slideUp();
			    	},10000);
				} else {
					$('#error').slideDown();
					setTimeout(function(){
						$('#error').slideUp();
			    	},10000);
				}
			 	
			}catch(err){
				console.log('TRY-CATCH error');
			}
			$('body').css('cursor','default');
			$('#send-btn').removeClass('disabled');
	    },
		error: function(){
			$('body').css('cursor','default');
			$('#send-btn').removeClass('disabled');
			console.log('AJAX error');
      	}
	});
	
}

function sendStatusEmail(TO, PAYMENTID, STATUS){
	var email_to = encodeURIComponent( $('#'+ TO).val() );
	var content = encodeURIComponent( $('#'+ PAYMENTID).val() );
	var subject = encodeURIComponent( $('#'+ STATUS).val() );
	var datastring = 'email='+email_to+'&payment_id='+content+'&status='+subject+'&formToken='+$('#formToken').val();
	$('body').css('cursor','wait');
	$('#send-btn').addClass('disabled');
	$.ajax({
		type: "POST",
	    url: "/admin/includes/processes/processes-email.php",
		cache: false,
		async: false,
		data: datastring,
		dataType: "html",
	    success: function(data) {
	    	try{
	    		var obj = $.parseJSON(data);
			 	if (obj.response) {
				 	$('.notification').hide();
			 		$('#sent').slideDown();
					setTimeout(function(){
						$('#sent').slideUp();
			    	},10000);
				} else {
					$('#error').slideDown();
					setTimeout(function(){
						$('#error').slideUp();
			    	},10000);
				}
			 	
			}catch(err){
				console.log('TRY-CATCH error');
			}
			$('body').css('cursor','default');
			$('#send-btn').removeClass('disabled');
	    },
		error: function(){
			$('body').css('cursor','default');
			$('#send-btn').removeClass('disabled');
			console.log('AJAX error');
      	}
	});
}



</script>
{/block}
